package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.entity.erp.ErpBarcodeAttribute;
import com.nuoniu.sibanyun.mapper.erp.ErpBarcodeAttributeMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpBarcodeAttributeBoService;
import com.nuoniu.sibanyun.service.erp.IErpBarcodeAttributeService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 条码管理-条码属性 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2021-11-03
 */
@Service
public class ErpBarcodeAttributeServiceImpl extends ServiceImpl<ErpBarcodeAttributeMapper, ErpBarcodeAttribute> implements IErpBarcodeAttributeService {

    @Autowired
    private IErpAccountService accountService;
    @Autowired
    private IErpBarcodeAttributeBoService attributeBoService;

    @Override
    public List<ErpBarcodeAttribute> getList() {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        return list(wrapper);
    }

    @Override
    public void saveAttribute(ErpBarcodeAttribute attribute) {
        UserInfo userInfo = UserThreadLocal.get();
        if(attribute.getId() == null) {
            attribute.setCompanyId(userInfo.getCompanyId());
            attribute.setAccountId(accountService.getCurrentAccountId());
            attribute.setCreateUserId(userInfo.getUserId());
            attribute.setCreateUserName(userInfo.getRealName());
        } else {
            attribute.setUpdateUserId(userInfo.getUserId());
            attribute.setUpdateUserName(userInfo.getRealName());
            attribute.setUpdateDate(LocalDateTime.now());
        }

        saveOrUpdate(attribute);

        //保存属性业务
        List<Long> detailIds = new ArrayList<>();
        attribute.getBarcodeAttributeBos().forEach(b -> {
            if(b.getId() != null) {
                detailIds.add(b.getId());
            } else {
                b.setBarcodeAttributeId(attribute.getId().intValue());
            }
        });

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("barcode_attribute_id", attribute.getId());
        if(detailIds.size() > 0) {
            wrapper.notIn("id", detailIds);
        }

        attributeBoService.remove(wrapper);
        attributeBoService.saveOrUpdateBatch(attribute.getBarcodeAttributeBos());
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("barcode_attribute_id", id);
        attributeBoService.remove(wrapper);
    }

    @Override
    public ErpBarcodeAttribute getDetail(Long id) {
        ErpBarcodeAttribute attribute = this.getById(id);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("barcode_attribute_id", id);
        attribute.setBarcodeAttributeBos(attributeBoService.list(wrapper));
        return attribute;
    }
}
